﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using VietKinsNutrition.Data;
using DevExpress.XtraEditors;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using VietKinsNutrition.Common;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

namespace VietKinsNutrition.Bussiness
{
    public class ClsTblDm_ThuvienmonanBLL : ClsBindingBLL
    {
        private int _id;
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        public void Bind_DotuoiIdCbo(ComboBoxEdit Cbo)
        {
            // Gán tất cả;
            DataTable Set;
            ClsTblDm_DotuoiDAL ClsTblDm_Dotuoidal = new ClsTblDm_DotuoiDAL();
            Set = ClsTblDm_Dotuoidal.SelectAll();
            DataTable Newset = Set.Clone();
            object[] obj = new object[] { 0, 0, 0, 0, "-Dùng chung-" };
            Newset.Rows.Add(obj);
            foreach (DataRow Row in Set.Rows)
            {
                Newset.Rows.Add(Newset.Rows.Count + 1, Row["Id"], Row["Tuoicanduoi"], Row["Tuoicantren"], Row["Tendotuoi"]);
            }
            UserComboBoxItem.SetItem(Cbo, Newset, "Id", "Tendotuoi");
        }

        public void ClearControl(TextEdit tenmonanTextBox, ComboBoxEdit loaimonIdComboBox, TextEdit motasudungTextBox, TextEdit nguyenlieuTextBox, PictureEdit HinhanhpictureBox, TextEdit thuchienTextBox, TextEdit chuthichTextBox, TextEdit NguontrichtextBox)
        {
            tenmonanTextBox.Text = motasudungTextBox.Text = nguyenlieuTextBox.Text = thuchienTextBox.Text = chuthichTextBox.Text = NguontrichtextBox.Text = "";
            HinhanhpictureBox.Image = null;
        }

        public DataSet GetByTopN_Id()
        {
            return this.ClsTblDm_Thuvienmonandal.GetByTopN_Id();
        }

        public void Bind_LoaimonIdCbo(ComboBoxEdit Cbo)
        {
            ClsTblDm_LoaimonDAL ClsTblDm_Loaimondal = new ClsTblDm_LoaimonDAL();
            DataTable dTable = ClsTblDm_Loaimondal.SelectAll();
            UserComboBoxItem.SetItem(Cbo, dTable, "Id", "Tenloaimon");
        }

        ClsTblDm_ThuvienmonanDAL ClsTblDm_Thuvienmonandal = new ClsTblDm_ThuvienmonanDAL();
        public bool FindSameName(int iBtn, int LoaimonId, int DotuoiId, string VarTen)
        {
            bool Value = false;
            DataTable dt;
            dt = ClsTblDm_Thuvienmonandal.FindSameName(LoaimonId, DotuoiId, VarTen).Tables[0];
            switch (iBtn)
            {
                case 1:
                    {
                        if (dt.Rows.Count >= 1)
                        {
                            XtraMessageBox.Show("Không cho phép thêm trùng dữ liệu", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Question);
                            Value = false;
                        }
                        else
                        {

                            Value = true;
                        }
                        break;
                    }
                case 2:
                    {
                        if (dt.Rows.Count == 0)
                        {
                            return true;
                        }
                        DataRow Row = dt.Rows[0];

                        if (dt.Rows.Count >= 1 && this.Id != int.Parse(Row["Id"].ToString()))
                        {
                            XtraMessageBox.Show("Tên không cho phép trùng", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Question);
                            Value = false;
                        }
                        else
                        {
                            Value = true;
                        }
                        break;
                    }
            }
            return Value;
        }

        public void FocusControl(TextBox Textbox, DataGridView Grid)
        {
            Textbox.Focus();
            Grid.Enabled = !Grid.Enabled;
        }

        public void BingSource_InputControl(TextEdit tenmonanTextBox, ComboBoxEdit loaimonIdComboBox, ComboBoxEdit DotuoiIdCbo,
            TextEdit motasudungTextBox, TextEdit nguyenlieuTextBox, PictureEdit HinhanhpictureBox,
            TextEdit thuchienTextBox, TextEdit chuthichTextBox, TextEdit Trichnguon,
            GridView Grid)
        {
            DirectoryInfo df = new DirectoryInfo(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\images\");
            if (Grid.RowCount > 0)
            {
                this.Id = int.Parse(Grid.GetFocusedRowCellValue("Id").ToString());
                tenmonanTextBox.Text = Grid.GetFocusedRowCellValue("Tenmonan").ToString();
                UserComboBoxItem.SelectItem(loaimonIdComboBox, Grid.GetFocusedRowCellValue("LoaimonId").ToString());
                motasudungTextBox.Text = Grid.GetFocusedRowCellValue("Motasudung").ToString();
                if (Grid.GetFocusedRowCellValue("DotuoiId").ToString().Trim() != "0")
                {
                    UserComboBoxItem.SelectItem(DotuoiIdCbo, Grid.GetFocusedRowCellValue("DotuoiId").ToString());
                }
                nguyenlieuTextBox.Text = Grid.GetFocusedRowCellValue("Nguyenlieu").ToString();
                if (Grid.GetFocusedRowCellValue("Hinhanh").ToString() != "")
                {
                    if (df.Exists)
                    {
                        DirectoryInfo dffile = new DirectoryInfo(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\images\" + Grid.GetFocusedRowCellValue("Hinhanh").ToString());
                        if (File.Exists(dffile.FullName.ToString()))
                        {
                            HinhanhpictureBox.Image = Image.FromFile(Directory.CreateDirectory(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\images\").FullName + Grid.GetFocusedRowCellValue("Hinhanh").ToString());
                        }
                        else
                        {
                            HinhanhpictureBox.Image = null;
                        }
                    }
                    else
                    {
                        HinhanhpictureBox.Image = null;
                    }
                }
                else
                {
                    HinhanhpictureBox.Image = null;
                }
                thuchienTextBox.Text = Grid.GetFocusedRowCellValue("Thuchien").ToString();
                chuthichTextBox.Text = Grid.GetFocusedRowCellValue("Chuthich").ToString();
                Trichnguon.Text = Grid.GetFocusedRowCellValue("Trichnguon").ToString();
            }
        }

        public void BingSource_DataGrid(int LoaimonId, int DotuoiId, GridControl GridView)
        {
            GridView.DataSource = ClsTblDm_Thuvienmonandal.SelectAllByLoaimonId(LoaimonId, DotuoiId);
        }

        public void BingSource_DataGrid(GridControl GridView)
        {
            GridView.DataSource = ClsTblDm_Thuvienmonandal.SelectAllByLoaimonId();
        }

        public void Insert(string Tenmonan, int LoaimonId, int DotuoiId, string Motasudung, string Nguyenlieu, string Thuchien, string Hinhanh, string Chuthich, string Trichnguon)
        {
            ClsTblDm_Thuvienmonandal.Insert(Tenmonan, LoaimonId, DotuoiId, Motasudung, Nguyenlieu, Thuchien, Hinhanh, Chuthich, Trichnguon);
        }

        public void Edit(int Id, string Tenmonan, int DotuoiId, int LoaimonId, string Motasudung, string Nguyenlieu, string Thuchien, string Hinhanh, string Chuthich, string Trichnguon)
        {
            ClsTblDm_Thuvienmonandal.Edit(Id, Tenmonan, DotuoiId, LoaimonId, Motasudung, Nguyenlieu, Thuchien, Hinhanh, Chuthich, Trichnguon);
        }
        public void Del()
        {
            ClsTblDm_Thuvienmonandal.Del(Id);
        }
        public DataSet DsetSelectAllByLoaimonId(int LoaimonId)
        {
            return ((DataSet)this.ClsTblDm_Thuvienmonandal.SelectAllByLoaimonId(LoaimonId).DataSource);
        }
    }
}
